package com.example.demo.util;
import com.ctrip.framework.apollo.Config;
import com.ctrip.framework.apollo.ConfigService;
import com.ctrip.framework.apollo.ConfigChangeListener;
import com.ctrip.framework.apollo.model.ConfigChange;
import com.ctrip.framework.apollo.model.ConfigChangeEvent;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

public class ApolloConfigUtil {

    private static String COOKIE = "";
    public static void main(String[] args) {
        //        // 1. 获取配置
        Config config = ConfigService.getAppConfig(); // 获取当前应用的默认配置

        // 2. 获取配置项
        COOKIE = config.getProperty("xhs_cookie", "default_value");

//        COOKIE = xhsCookie;
        System.out.println("Initial config value: " + COOKIE);

        // 3. 配置变更监听，监听配置变化
        config.addChangeListener(new ConfigChangeListener() {
            @Override
            public void onChange(ConfigChangeEvent changeEvent) {
                for (String changedKey : changeEvent.changedKeys()) {
                    ConfigChange change = changeEvent.getChange(changedKey);
                    COOKIE = change.getNewValue();
//                    if(""changedKey)
                    System.out.println("Config changed: " + changedKey + ", old value: " + change.getOldValue() + ", new value: " + change.getNewValue());
                }
            }
        });

        // 模拟等待一段时间，以接收配置更新
        try {
            Thread.sleep(60000);  // 等待一段时间以接收配置变化
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("COOKIE:"+COOKIE);
    }

}
